(function($) {
	$.fn.extend({
		replyTo : function(options) {
			var defaults = {
				listURL : "brand/replyAdress/list",
				deleteURL : "brand/replyAdress/delete",
				updateTo : "replyTo",
			};
			this.options = $.extend(defaults, options);
			_this = this;
			$this = $(this);

			this.reload = function(data) {
				$.ajax({
					type : 'POST',
					url : this.options.listURL,
					context : this,
					async : 'false',
					success : function(data) {
						_this.reloadCallback(data);
					},
					error : function(data) {
						alert("Server Error");
					}
				});
				return $(this);
			};

			this.reloadCallback = function(data) {
				var $menuNode = $this.find(".dropdown-menu");
				$menuNode.empty();
				for(var i = 0; i < data.length; i++) {
					$menuNode.append(this.getTemplate(data[i]));
				}
			};

			this.getTemplate = function(data) {
				var template = "<li>";
				template += "<a href='javascript: void(0);' data-id='";
				template += data.id;
				template += "' data-name='";
				template += data.name;
				template += "' data-address='";
				template += data.address;
				template += "' class='selectButton'>";
				template += data.name;
				template += "</a>";
				template += "<a href='javascript: void(0);' data-id='";
				template += data.id;
				template += "' class='deleteButton'>&times;</a>";
				template += "</li>";

				return template;
			};
			if(!this.inited) {
				this.inited = true;
				this.reload();

				$(this).find(".selectButton").live("click", function() {
					$("#" + _this.options.updateTo).val($(this).attr("data-address"));
				});
				$(this).find(".deleteButton").live("click", function() {
					$.ajax({
						type : 'POST',
						url : _this.options.deleteURL,
						data: "id=" + $(this).attr("data-id"),
						context : this,
						async : 'false',
						success : function(data) {
							$(this).parent("li").remove();
						},
						error : function(data) {
							alert("Server Error");
						}
					});

				});

				$(this).find(".newButton").click(function() {
					$("#newId").val("");
					$("#newName").val("");
					$("#newAddress").val("");
					$("#alertMsgInModel").hide();
					$("#newBrandModel").modal();
					return false;
				});

				$("#newBrandModel").find(".saveButton").click(function() {
					$("#newBrandReplyForm").validate();
					if(!$("#newBrandReplyForm").valid()){
						return false;
					}
					var name = $("#newName").val();
					var address = $("#newAddress").val();
					
					var url = "brand/savebrandreply?name=" + name + "&address=" + address;
					$.ajax({
						type : 'POST',
						url : url,
						success : function(returnMessage) {
							if(returnMessage == "true") {
								$("#newBrandModel").modal("hide");
								_this.reload();
							} else if(returnMessage == "false"){
								$("#alertMsgInModel").show();
							}
						}
					});
				});
			}
		}
	});
})(jQuery);
